import { fetchHotList, getByConfigKey } from '@/api'

export default {
  data() {
    return {
      productList: [],
      categoryInfo: null,
      categoryId: null,
      brandId: null,
      pageTitle: '回收专场',
      promoImage: ''
    }
  },
  
  onLoad(options) {
    // 获取URL参数
    this.categoryId = options.categoryId || this.getDefaultCategoryId()
    this.brandId = options.brandId || null
    
    this.initPageData()
  },
  
  methods: {
    // 子页面需要重写此方法返回默认的分类ID
    getDefaultCategoryId() {
      return '100001'
    },
    
    // 子页面需要重写此方法返回默认的分类名称
    getDefaultCategoryName() {
      return '产品'
    },
    
    async initPageData() {
      try {
        // 并行获取配置信息和热门商品数据
        const [configResult, hotListResult] = await Promise.all([
          this.getCategoryConfig(),
          this.getHotProducts()
        ])
        
        // 匹配分类信息并设置页面标题和图片
        this.matchCategoryInfo(configResult)
        
        // 处理热门商品数据
        this.processHotProducts(hotListResult)
        
      } catch (error) {
        console.error('初始化页面数据失败:', error)
        uni.showToast({
          title: '数据加载失败',
          icon: 'none'
        })
      }
    },
    
    async getCategoryConfig() {
      try {
        const result = await getByConfigKey('special_list')
        return result.data || []
      } catch (error) {
        console.error('获取分类配置失败:', error)
        return []
      }
    },
    
    async getHotProducts() {
      try {
        const params = {
          categoryId: this.categoryId
        }
        if (this.brandId) {
          params.brandId = this.brandId
        }
        
        const result = await fetchHotList(params)
        return result.data || []
      } catch (error) {
        console.error('获取热门商品失败:', error)
        return []
      }
    },
    
    matchCategoryInfo(configData) {
     const categoryConfig = JSON.parse(configData.configValue || '[]')


      if (categoryConfig && categoryConfig.length > 0) {
        const matchedCategory = categoryConfig.find(item => 
          item.categoryId === this.categoryId
        )
        
        if (matchedCategory) {
          this.categoryInfo = matchedCategory
          this.pageTitle = matchedCategory.categoryName + '回收专场'
          this.promoImage = matchedCategory.image
          
          // 设置页面标题
          uni.setNavigationBarTitle({
            title: this.pageTitle
          })
        } else {
          // 使用默认配置
          this.setDefaultPageConfig()
        }
      } else {
        // 使用默认配置
        this.setDefaultPageConfig()
      }
    },
    
    setDefaultPageConfig() {
      this.pageTitle = this.getDefaultCategoryName() + '回收专场'
      this.promoImage = this.getDefaultPromoImage()
      
      // 设置页面标题
      uni.setNavigationBarTitle({
        title: this.pageTitle
      })
    },
    
    // 子页面需要重写此方法返回默认的宣传图片
    getDefaultPromoImage() {
      return 'https://image.chushouya.com/assets/images/special/default.png'
    },
    
    processHotProducts(hotProducts) {
      if (hotProducts && hotProducts.length > 0) {
        this.productList = hotProducts.map(product => ({
          id: product.productId,
          name: product.productName,
          price: product.maxPrice ? product.maxPrice.toFixed(0) : '0',
          image: product.productImage || this.promoImage,
          brandName: product.brandName,
          categoryName: product.categoryName
        }))
      } else {
        // 如果没有热门商品数据，使用默认数据
        this.setDefaultProducts()
      }
    },
    
    setDefaultProducts() {
      this.productList = []
    },
    
    goBack() {
      uni.navigateBack()
    },
    
    startRecycle(product) {
      console.log('开始回收:', product.name)
      // 跳转到回收详情页
      uni.navigateTo({
        url: `/pages/product/index?id=${product.id}`
      })
    },
    
    showMoreModels() {
      console.log('显示更多机型')
      // 跳转到更多机型页面
      uni.navigateTo({
        url: `/pages/category/index?categoryId=${this.categoryId}`
      })
    }
  }
}
